package cn.bdqn.controller;

import cn.bdqn.domain.User;
import cn.bdqn.service.UserService;
import cn.bdqn.service.impl.UserServiceImpl;
import cn.bdqn.utils.result.Page;
import cn.bdqn.utils.result.Result;
import cn.bdqn.utils.result.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService us;

    /**
     * 添加角色
     * @param user 获取页面请求的信息
     * @return
     * @throws Exception
     */
    @RequestMapping("/addUser")
    public Result addUser(User user) throws Exception{

        try {
            System.out.println(user);
            user.setState(0);
            us.addUser(user);
            return ResultGenerator.genSuccessResult(user);
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }

    /**
     * 根据id查询用户信息
     * @param id id
     * @return 用户信息
     * @throws Exception
     */
    @RequestMapping("/getUserInfo")
    public Result queryUserById(Integer id) throws Exception{
        try {
            User user = us.selectById(id);
            return ResultGenerator.genSuccessResult(user);
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }

    /**
     * 更新当前角色
     * @param user
     * @return
     * @throws Exception
     */
    @RequestMapping("/updateUser")
    public Result editUser(User user) throws Exception{

        try {
            us.updateUser(user);
            return ResultGenerator.genSuccessResult(user);
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }

    /**
     * 删除指定用户
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/remove")
    public Result removeUser(Integer id) throws Exception{
        try {
            us.deleteUserById(id);
            List<User> users = us.queryAll();
            return ResultGenerator.genSuccessResult("用户已删除!");
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }

    /**
     * 登录
     * @param user
     * @return
     * @throws Exception
     */
    @RequestMapping("/login")
    public Result queryByInfo(User user) throws Exception{
        try {
            User resultUser = us.selectUserByInfo(user);
            return ResultGenerator.genSuccessResult(resultUser);
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }

    /**
     * 判断当前电话是否存在
     * @param phone 电话号
     * @return  返回结果   true 存在    false不存在
     * @throws Exception
     */
    @RequestMapping("/phoneIsExist")
    public Result existByPhone(String phone) throws Exception{
        try {
            Boolean result = us.selectUserByPhone(phone);
            return ResultGenerator.genSuccessResult(result);
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }

    /**
     * 判断当前邮箱是否存在
     * @param email 邮箱号
     * @return  返回结果   true 存在    false不存在
     * @throws Exception
     */
    @RequestMapping("/emailIsExist")
    public Result existByEmail(String email) throws Exception{
        try {
            Boolean result = us.selectUserByEmail(email);
            return ResultGenerator.genSuccessResult(result);
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }

    /**
     * 查询指定机构下的角色 分页
     * @param startNumber 起始页
     * @param groupId  指定的机构
     * @return 分页结果
     * @throws Exception
     */
    @RequestMapping("/queryGroupByPages")
    public Result queryGroupByPages(Integer startNumber,Integer pageSize,Integer groupId) throws Exception{
        try {
            Page<User> userPage = us.selectUsersByGroupId(startNumber,pageSize,groupId);
            return ResultGenerator.genSuccessListResult(userPage.getMenuList(),userPage.getTotalRecord());
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }


}
